package cn.tedu.personal_patient.information.services.impl;

import cn.tedu.common.enumerator.ServiceCode;
import cn.tedu.common.ex.ServiceException;
import cn.tedu.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.personal_patient.information.dao.persist.repository.IPatientAddressRepository;
import cn.tedu.personal_patient.information.dao.persist.repository.IPatientRepository;
import cn.tedu.personal_patient.information.pojo.entity.Patient;
import cn.tedu.personal_patient.information.pojo.entity.PatientAddress;
import cn.tedu.personal_patient.information.pojo.param.PatientAddNewParam;
import cn.tedu.personal_patient.information.pojo.param.PatientUpdateParam;
import cn.tedu.personal_patient.information.pojo.vo.OutPatient;
import cn.tedu.personal_patient.information.pojo.vo.PatientListVO;
import cn.tedu.personal_patient.information.services.IPatientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class PatientServiceImpl implements IPatientService {
    @Autowired
    private IPatientRepository patientRepository;
    @Autowired
    private IPatientAddressRepository patientAddressRepository;

    @Override
    public List<PatientListVO> listByUserId(CurrentPrincipal currentPrincipal) {
        return patientRepository.listByUserId(currentPrincipal.getId());
    }

    @Override
    public void insert(CurrentPrincipal currentPrincipal,PatientAddNewParam patientAddNewParam) {
        Patient patient = new Patient();
        PatientAddress patientAddress=new PatientAddress();
        BeanUtils.copyProperties(patientAddNewParam,patient);
        BeanUtils.copyProperties(patientAddNewParam,patientAddress);
        patient.setUserId(currentPrincipal.getId());
        patient.setGmtCreate(LocalDateTime.now());
        patient.setGmtModified(LocalDateTime.now());
        patientAddress.setUserId(currentPrincipal.getId());
        patientAddress.setGmtCreate(LocalDateTime.now());
        patientAddress.setGmtModified(LocalDateTime.now());
        int insert = patientRepository.insert(patient);
        if (insert != 1) {
            String message = "添加失败，请稍后再试";
            throw new ServiceException(ServiceCode.ERROR_INSERT,message);
        }
        patientAddress.setPatientId(patient.getId());
        int insert1 = patientAddressRepository.insert(patientAddress);
        if (insert1 != 1) {
            String message = "添加失败，请稍后再试";
            throw new ServiceException(ServiceCode.ERROR_INSERT,message);
        }
    }

    @Override
    public void update(Long id, PatientUpdateParam patientUpdateParam) {
        Patient patient = new Patient();
        PatientAddress patientAddress=new PatientAddress();
        BeanUtils.copyProperties(patientUpdateParam,patient);
        BeanUtils.copyProperties(patientUpdateParam,patientAddress);
        patient.setId(id);
        patientAddress.setPatientId(id);
        int update = patientRepository.update(patient);
        if (update != 1) {
            String message = "修改失败，请稍后再试";
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
        int updateAddress = patientRepository.updateAddress(patientAddress);
        if (updateAddress != 1) {
            String message = "修改失败，请稍后再试";
            throw new ServiceException(ServiceCode.ERROR_UPDATE,message);
        }
    }

    @Override
    public int delete(Long id) {
        return patientRepository.delete(id);
    }

    @Override
    public List<OutPatient> listUserId(CurrentPrincipal currentPrincipal) {
        return patientRepository.listUserId(currentPrincipal.getId());
    }
}
